#!/usr/bin/python
import sys

# include lib paths
sys.path.insert(0, '/home/msg-seer/lib')
sys.path.insert(0, '/home/pro/svn/projects/txtdoc/lib/')

import re

from content import MsgContent

msg_content = MsgContent(None, 'msgs_content')

f = open( '/tmp/content.csv', 'r')

for line in f:
    # NOTE
    # - must be TAB delimited
    # line format: name, message, tags, lom
    p = re.search('^(.*)\t(.*)\t(.*)\t(.*)\n$', line)

    # remove spaces from start / end of text
    name    = re.sub("\A\s*|\s*\Z", '', p.group(1))
    message = re.sub("\A\s*|\s*\Z", '', p.group(2))
    tags    = re.sub("\A\s*|\s*\Z", '', p.group(3))

    # remove multiple spaces in the text
    name    = re.sub("\s{2,}", ' ', name)
    message = re.sub("\s{2,}", ' ', message)
    tags    = re.sub("\s{2,}", ' ', tags)

    # check if content is already in DB
    rec = msg_content.search_by_name(name)

    if rec is None:
        print 'Not in DB: ' + name.upper()

        # insert
        msg_content.insert_content(name.upper(), message, tags.upper(), '8211') 
    else:
        print 'Found in DB: ' + name.upper()
